#include "bm_list.h"
#include <synlib.h>


UINT32 BM_Init( BM_LIST_T * bml )
{
	bml->count = 0;
	bml->list = NULL;
	return RESULT_OK;
}

UINT32 BM_Free( BM_LIST_T * bml )
{
	if ( bml->list ) {
		suFreeMem( bml->list );
		bml->list = NULL;
	}
	bml->count = 0;
	
	return RESULT_OK;
}

UINT32 BM_Alloc( BM_LIST_T * bml, UINT8 c )
{
	BM_T *		tmp;
	INT32		err;
	
	if ( c > BOOKMARK_MAX_COUNT )
		return RESULT_FAIL;
	tmp = (BM_T *) suAllocMem(sizeof(BM_T)*c, &err);
	if ( err != RESULT_OK )
		return err;
	bml->count = c;
	if ( bml->list )
		suFreeMem( bml->list );
	bml->list = tmp;
	
	return RESULT_OK;
}

UINT32 BM_Append( BM_LIST_T * bml, WCHAR * uri, UINT16 attr )
{
	BM_T *			list;
	UINT32			i;
	INT32			err;
	
	if ( bml->count == BOOKMARK_MAX_COUNT )
		return RESULT_FAIL;
	list = (BM_T *) suAllocMem( (bml->count + 1)*sizeof(BM_T), &err );
	if ( err == RESULT_OK ) {
		for ( i=0; i<bml->count; i++ ) {
			memcpy( &list[i], &bml->list[i], sizeof(BM_T) );
		}
		u_strcpy(list[i].uri, uri);
		list[i].attr = attr;
		if ( bml->list ) {
			suFreeMem( bml->list );
		}
		bml->list = list;
		bml->count++;
	} else {
		return RESULT_FAIL;
	}
	return RESULT_OK;
}

UINT32 BM_Delete( BM_LIST_T * bml, UINT8 idx )
{
	if ( idx >= bml->count )
		return RESULT_FAIL;
	if ( bml->count == 1 )
		return BM_Free(bml);
	while ( idx < bml->count - 1 ) {
		memcpy(&bml->list[idx], &bml->list[idx+1], sizeof(BM_T));
		idx++;
	}
	bml->count--;
	return RESULT_OK;
}
